/**
 * Author : bianlongting
 * Email  : 1137060420@qq.com
 * Date   : 2015-12-30
 */

var blt =  new Base();	//前台调用

function Base(){

};

Base.prototype.getId = function(id){
	return document.getElementById(id);
};

/*
 * 截取字符串
 * 例 : var test = "bianlongting"; blt.cutstr(test,2) => "bi..."
 */
Base.prototype.cutstr = function(str, len) {
	var temp,
	icount = 0,
	patrn = /[^\x00-\xff]/,
	strre = "";
	if (str.length > len) {
		for (var i = 0; i < str.length; i++) {
			if (icount < len - 1) {
				temp = str.substr(i, 1);
				if (patrn.exec(temp) == null) {
					icount = icount + 1;
				} else {
					icount = icount + 2;
				}
				strre += temp;
			} else {
				break;
			}
		}
		return strre + "..."
	}else{
		return str;
	}
}

/*
 * 去掉左边的空格
 * 例 : var test = " bi an "; blt.ltrim(test) => "bi an  ";
 */
Base.prototype.ltrim = function(str){
	return str.replace( /^(\s*|　*)/, "");
}

/*
 * 去掉右边的空格
 * 例 : var test = " bi an "; blt.rtrim(test) => "  bi an";
 */
Base.prototype.ltrim = function(str){
	return str.replace( /(\s*|　*)$/, "");
}

/*
 * 浮点型相加
 * 例 : blt.addFloat(0.1,0.2) => 0.3;
 */

Base.prototype.addFloat = function(arg1,arg2){
	var r1,r2,m; 
	try { r1=arg1.toString().split(".")[1].length } catch(e){ r1=0 };
	try { r2=arg2.toString().split(".")[1].length } catch(e){ r2=0 };
	m = Math.pow(10,Math.max(r1,r2)) ;
	return (arg1*m+arg2*m)/m;
};

// 0.1.add(0.2)
Number.prototype.add = function (arg){ 
	return blt.addFloat(this, arg); 
}

/*
 * 浮点型相除
 * 例 : blt.divFloat(0.01,0.2) => 0.05;
 */

Base.prototype.divFloat = function(arg1,arg2){ 
	var t1=0,t2=0,r1,r2; 
	try{t1=arg1.toString().split(".")[1].length}catch(e){} 
	try{t2=arg2.toString().split(".")[1].length}catch(e){} 
	with(Math){ 
		r1=Number(arg1.toString().replace(".","")) 
		r2=Number(arg2.toString().replace(".","")) 
		return (r1/r2)*pow(10,t2-t1); 
	} 
};

// 0.01.div(0.2)
Number.prototype.div = function (arg){ 
	return blt.divFloat(this, arg); 
}

/*
 * 浮点型相乘
 * 例 : blt.divFloat(0.1,0.1) => 0.01;
 */

Base.prototype.mulFloat = function(arg1,arg2) { 
	var m=0,s1=String(arg1).toString(),s2=String(arg2).toString(); 
	try{m+=s1.split(".")[1].length}catch(e){} 
	try{m+=s2.split(".")[1].length}catch(e){} 
	return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
};

//0.1.mul(0.1)
Number.prototype.mul = function (arg){ 
	return blt.mulFloat(arg, this); 
}


/*
 * 判断是否是数字
 * 例 : var test = "2132",str = "bian"; blt.isNum(test) => true  blt.isNum(str) => false;
 */
Base.prototype.isNum = function(num){
	var reg = /^[0-9]*$/;
	if (reg.exec(num) == null || num == "") {
		return false;
	}else{
		return true;
	}
}

/*
 * 数组去重
 * 例 : var test = [1,2,3,4,1,2],blt.unique(test);
 */
Base.prototype.unique = function(arr) {
	var result = []; 
	for (var i = 0; i < arr.length; i++){
	  if (result.indexOf(arr[i]) == -1) result.push(arr[i]);
	}
	return result;
}

/*
 * 数组交集
 * 例 : var test = [1,2,3,4],test1 = [1,2,4],blt.jj(test,test1) => [1,2,4];
 */
Base.prototype.jj = function(a, b) { 
    var result = [];
    for(var i = 0; i < b.length; i ++) {
        var temp = b[i];
        for(var j = 0; j < a.length; j ++) {
            if(temp === a[j]) {
                result.push(temp);
                break;
            }
        }
    }
    return blt.unique(result);
}

/*
 * 数组并集
 * 例 : var test = [1,2,3,4],test1 = [1,2,4,5],blt.bj(test,test1) => [1,2,3,4,5];
 */
Base.prototype.bj = function(a, b) { 
    return blt.unique(a.concat(b));
}

/*
 * 数组差集
 * 例 : var test = [1,2,3,4],test1 = [1,2,4,5],blt.cj(test,test1) => [3];
 */
Base.prototype.cj = function(a, b) {
    var clone = a.slice(0);
    for(var i = 0; i < b.length; i ++) {
        var temp = b[i];
        for(var j = 0; j < clone.length; j ++) {
            if(temp === clone[j]) {
                clone.splice(j,1);
            }
        }
    }
    return blt.unique(clone);
}

/*
 * 设置cookie
 * 例 : blt.setCookie("bian","test");
 */
Base.prototype.setCookie = function(name, value, Hours) {
	var d = new Date();
	var offset = 8;
	var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
	var nd = utc + (3600000 * offset);
	var exp = new Date(nd);
	exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";"
}

/*
 * 获取cookie
 * 例 : blt.getCookie("bian");
 */

Base.prototype.getCookie = function(name) {
	var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
	if (arr != null) return unescape(arr[2]);
	return null;
}

/*
 * 获取URL参数
 */
Base.prototype.getParam = function(name){
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return unescape(r[2]); return null; 

}

/*
 * 加入收藏夹
 * 例 : blt.AddFavorite(window.location,document.title);
 */
Base.prototype.AddFavorite = function(sURL, sTitle) {
	try {
		window.external.addFavorite(sURL, sTitle)
	} 
	catch(e) {
		try {
			window.sidebar.addPanel(sTitle, sURL, "")
		} 
		catch(e) {
			alert("加入收藏失败，请使用Ctrl+D进行添加")
		}
	}
};

/*
 * 设置为首页
 * 例 : blt.setHomepage();
 */
Base.prototype.setHomepage = function() {
	if (document.all) {
		document.body.style.behavior = 'url(#default#homepage)';
		document.body.setHomePage('http://w3cboy.com')
	} else if (window.sidebar) {
		if (window.netscape) {
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")
			} catch(e) {
				alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true")
			}
		}
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage', 'http://w3cboy.com')
	}
}


/*
 * 字符串替换某一个
 * 例 : var test = "bianlongting"; test("i","b") => "bbanlongtbng";
 */

String.prototype.replaceAll = function(s1, s2) {
	return this.replace(new RegExp(s1, "gm"), s2)
}

/*
 * 替换空格
 * 例 : var test = " bi an "; test.trim() => "bi an";
 */

String.prototype.trim = function() {
	var reg = /^\s*(.*?)\s+$/;
	return this.replace(reg, "$1")
}

/*
 * 判断是否以某个字符串开头
 * 例 : var test = "bian"; test.startString("bi") => true;
 */

String.prototype.startString = function (str) {
	return this.indexOf(str) == 0
}

/*
 * 判断是否以某个字符串结束
 * 例 : var test = "bian"; test.endString("an") => true;
 */

String.prototype.endString = function (str) {
	var d = this.length - str.length;
	return (d >= 0 && this.lastIndexOf(str) == d)
}


